package cc.mrbird.unit.utils;

import cc.mrbird.system.domain.User;
import cc.mrbird.unit.domain.AuxiliaryInfor;
import cc.mrbird.unit.service.AuxiliaryInforService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

/**
 * 文件上传工具包
 */
@Component
@RestController
public class FileUtils {

    @Autowired
    private AuxiliaryInforService auxiliaryInforService;
    /**
     * @param fileOne 文件
     * @param fileTwo 文件
     * @param auxiliary_infor 对象
     * @return
     */
    public boolean upload(MultipartFile fileOne, MultipartFile fileTwo, AuxiliaryInfor auxiliary_infor,User user) {

        boolean result = false;
        //for (MultipartFile file : files) {
            String path = System.getProperty("user.dir"); //获取当前项目根目录
            path += File.separator + "src" + File.separator + "main" + File.separator + "resources" + File.separator
                    + "static" + File.separator + "img" + File.separator + "auxiliary";
            String fileNameOne = fileOne.getOriginalFilename();
            String fileNameTwo = fileTwo.getOriginalFilename();
            // 生成新的文件名
            String realPathOne = path + File.separator + FileNameUtils.getFileName(fileNameOne);
            String realPathTwo = path + File.separator + FileNameUtils.getFileName(fileNameTwo);
            /*  //使用原文件名
             String realPath = path + "/" + fileName;*/
            File destOne = new File(realPathOne);
            File destTwo = new File(realPathTwo);
            //判断文件父目录是否存在
            if (!destOne.getParentFile().exists()) {
                destOne.getParentFile().mkdir();
            }
            if (!destTwo.getParentFile().exists()) {
                destTwo.getParentFile().mkdir();
            }
            try {
                //保存文件
                fileOne.transferTo(destOne);
                fileTwo.transferTo(destTwo);
                auxiliary_infor.setFloor_plan(realPathOne);
                auxiliary_infor.setLocation_map(realPathTwo);
                result = true;
            } catch (Exception e) {
                e.printStackTrace();
                result = false;
            }
       // }
        if(result==true){
            auxiliaryInforService.add(auxiliary_infor,user);
        }
        return result ;
    }
    public boolean update(MultipartFile fileOne, MultipartFile fileTwo, AuxiliaryInfor auxiliary_infor) {
        boolean result = false;
        //for (MultipartFile file : files) {
        String path = System.getProperty("user.dir"); //获取当前项目根目录
        path += File.separator + "src" + File.separator + "main" + File.separator + "resources" + File.separator
                + "static" + File.separator + "img" + File.separator + "auxiliary";
        String fileNameOne = fileOne.getOriginalFilename();
        String fileNameTwo = fileTwo.getOriginalFilename();
        // 生成新的文件名
        String realPathOne = path + File.separator + FileNameUtils.getFileName(fileNameOne);
        String realPathTwo = path + File.separator + FileNameUtils.getFileName(fileNameTwo);
            /*  //使用原文件名
             String realPath = path + "/" + fileName;*/
        File destOne = new File(realPathOne);
        File destTwo = new File(realPathTwo);
        //判断文件父目录是否存在
        if (!destOne.getParentFile().exists()) {
            destOne.getParentFile().mkdir();
        }
        if (!destTwo.getParentFile().exists()) {
            destTwo.getParentFile().mkdir();
        }
        try {
            //保存文件
            fileOne.transferTo(destOne);
            fileTwo.transferTo(destTwo);
            auxiliary_infor.setFloor_plan(realPathOne);
            auxiliary_infor.setLocation_map(realPathTwo);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
            result = false;
        }
        // }
        if(result==true){
            auxiliaryInforService.updateauxiliary(auxiliary_infor);
        }
        return result ;
    }
}
